import $service from '../../../service.js';
const app = getApp();
let receptionState // 是否为接诊状态

Page({
  data: {
    titleList: [{
      name: '全部',
      val: '2'
    }, {
      name: '在线',
      val: '1'
    }, {
      name: '离线',
      val: '0'
    }],
    titleActive: '2',
  },
  onLoad() {
    app.editTabBar();
  },
  onShow() {
    receptionState = $service.getStore('isReception'); // 获取
    if (receptionState) {
      this.initWebSocket()
      this.sendType()
    }
  },
  initWebSocket() {
    app.webSocket.initWebSocket({
      url: $service.wsUrl
    });
    app.webSocket.onReceivedMsg(res => {
      if (res.packageType === 7) {
        this.handleReception(res)
      }
    })
  },
  // 点击接诊后消息处理
  handleReception(res) {
    let {
      inquiryType,
      patientIM,
      id,
      doctorId,
      patientName,
      doctorIM
    } = res.inquiryPatientInfoResp
    if (inquiryType == 1) {
      $service.setStore('tencentConfigD', JSON.stringify(res.tencentConfig))
    }
    wx.reLaunch({
      url: '/pages/chat/doctor_chat/doctor_chat?isAuto=0&logId=' + res.logId + '&imId=' + patientIM + '&inquiryPatientId=' + id + '&log_type=' + inquiryType + '&doctorId=' + doctorId + '&patientName=' + patientName + '&doctorIM=' + doctorIM
    })
  },
  selectTitle(e) {
    if (!receptionState) {
      $service.showToast('请先返回首页选择接诊方式')
      return
    }
    let {
      val
    } = e.currentTarget.dataset
    this.setData({
      titleActive: val
    })
    this.sendType()
  },
  onRefresh() {
    if (this._freshing) return
    this._freshing = true
    setTimeout(() => {
      this.setData({
        triggered: false,
      })
      this._freshing = false
    }, 1000)
  },
  sendType() {
    let params = {
      "doctorId": JSON.parse($service.getStore('userInfo')).id, //医生id
      "isOnline": this.data.titleActive, // 0-离线，1-在线，2-全部
      "packageType": 31
    }
    app.webSocket.sendWebSocketMsg({
      data: params,
      fail: () => {
        $service.showToast('操作失败')
      }
    })
  },
  //  接诊 分诊按钮
  ullistGotopage(e) {
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.num;
    let treatmentData = this.data.ullist[index];
    let inquiryType = e.currentTarget.dataset.inquirytype;
    if (inquiryType == 3 || inquiryType == 4) {
      wx.showModal({
        title: '家庭医生和复诊暂不接受分诊',
        showCancel: false
      })
    } else {
      if (num == 1) {
        $service.openPage('/pages/doctor_side/index/index_interrrogation_triage/index_interrrogation_triage?log_id=' + this.data.ullist[index].logId)
      } else if (num == 2) {
        app.webSocket.sendWebSocketMsg({
          data: {
            "logId": treatmentData.logId, // 问诊id，待接诊列表内已提供
            "packageType": 10 // 信息包类型
          }
        })
      }
    }
  },
})